import { useRequest } from "ahooks";
import { duplicateQuestionService } from "../services/question";
import { QUESTION_EDIT_PATHNAME } from "../router";
import { useNavigate } from "react-router-dom";

function useDuplicateQuestion() {
  const nav = useNavigate();
  const { loading: duplicateLoading, run: duplicate } = useRequest(
    async (id: string) => {
      const data = await duplicateQuestionService(id);
      return data;
    },
    {
      manual: true,
      onSuccess(result) {
        nav(QUESTION_EDIT_PATHNAME + result.id);
      },
    }
  );

  return { duplicateLoading, duplicate };
}

export default useDuplicateQuestion;
